package com.sfh.dog.ui;


import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import com.sfh.dog.ui.base.BaseActivity;
import com.sfh.dog.util.LogUtil;
import com.sfh.dog.util.PhotoUtil;
import com.sfh.dog.util.PhotoUtil.AsySearchPhoto;

/**
 * 拍照与图库选择
 * 
 */
public abstract class APhotoFileActivity extends BaseActivity
{
	private String mCameraFileName;

	public abstract void loadFilePath(String path);

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		if (savedInstanceState != null)
		{
			mCameraFileName = savedInstanceState.getString("mCameraFileName");
		}
	}

	/** 拍照 */
	public void openNewPhoto()
	{
		mCameraFileName = PhotoUtil
				.startActivityNowPic(APhotoFileActivity.this);
	}

	/** 打开相册 */
	public void openPhotoLib()
	{
		PhotoUtil.startActivityLib(APhotoFileActivity.this);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState)
	{
		outState.putString("mCameraFileName", mCameraFileName);
		super.onSaveInstanceState(outState);
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if (resultCode == Activity.RESULT_OK)
		{
			switch (requestCode)
			{
				case PhotoUtil.SELECT_PICTURE_FROM_CAMERA:
				{
					LogUtil.e("mCameraFileName=" +mCameraFileName);
					// 拍照
					File file = PhotoUtil.onActivityResultNowPhoto(data,
							mCameraFileName);
					if (null == file)
					{
//						showToast("拍照失败,文件无法获取!");
						return;
					}
					loadFilePath(file.getAbsolutePath());
					break;
				}
				case PhotoUtil.SELECT_PICTURE_FROM_FILE:
				{
					// 图库
					Uri selectedImageUri = data.getData();
					new AsySearchPhoto(APhotoFileActivity.this,
							new PhotoUtil.IPhoto()
							{

								@Override
								public void getPhoto(String path)
								{
									if (path == null || "".equals(path))
									{
										return;
									}

									loadFilePath(path);
								}
							}).execute(selectedImageUri);
					break;
				}
				default:
					break;
			}
		}

		super.onActivityResult(requestCode, resultCode, data);
	}
}
